package com.bosssoft.boss_exam_db.service.busines.impl;

import com.bosssoft.boss_exam_db.entity.Admin;
import com.bosssoft.boss_exam_db.entity.Company;
import com.bosssoft.boss_exam_db.entity.Department;
import com.bosssoft.boss_exam_db.mapper.AdminMapper;
import com.bosssoft.boss_exam_db.mapper.DepartmentMapper;
import com.bosssoft.boss_exam_db.model.vo.CascaderVO;
import com.bosssoft.boss_exam_db.service.base.impl.BaseServiceImpl;
import com.bosssoft.boss_exam_db.service.busines.DepartmentService;
import com.bosssoft.boss_exam_db.util.MySqlUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {

    //注入DepartmentMapper
    @Autowired
    private DepartmentMapper departmentMapper;

    //注入管理员服务
    @Autowired
    private AdminMapper adminMapper;


    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/18
     * @Description 根据当前用户所在公司，部门名称，父部门名称查询查询
     * @Param [company, deparmentName, parentId, leaderName, page, limit, sort, order]
     * @Return com.github.pagehelper.PageInfo<com.bosssoft.boss_exam_db.model.vo.DepartmentVO>
     * @Exception 无
     * @since JDK 1.8
     **/
    @Override
    public PageInfo<Department> queryByCondition(Company company, String deparmentId, String parentId, String leaderName, Integer page, Integer limit, String sort, String order) {
        Example example = new Example(Department.class);
        Example.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(company.getId())){
            criteria.andEqualTo("companyId", company.getId());
        }
        if(!StringUtils.isEmpty(deparmentId)){
            criteria .andEqualTo("id",deparmentId);
        }
        if(!StringUtils.isEmpty(parentId)&&StringUtils.isEmpty(deparmentId)){
            criteria .andEqualTo("parentId", parentId);
        }
        List<Admin> admins =null;
        //根据管理员名称模糊查询管理员
        if(!StringUtils.isEmpty(leaderName)){
            Example adminExample = new Example(Admin.class);
            Example.Criteria adminExampleCriteria = adminExample.createCriteria();
            if(!StringUtils.isEmpty(company.getId())){
                adminExampleCriteria.andEqualTo("companyId", company.getId());
            }
            adminExampleCriteria.andLike("username",MySqlUtil.cloumLike(leaderName));
            adminExampleCriteria.andEqualTo("deleted",false);
            admins= adminMapper.selectByExample(adminExample);
        }
        System.out.println(admins);
        if(admins!=null&&admins.size()!=0){
            List<String> adminIds = admins.stream().map(admin -> admin.getId()).collect(Collectors.toList());
            if(adminIds.size()!=0&&adminIds!=null){
                criteria.andIn("leaderId",adminIds);
            }
        }
        criteria  .andEqualTo("deleted", false);
        List<Department> departments = this.departmentMapper.selectByExample(example);
        PageHelper.startPage(page, limit).setOrderBy(sort + " " + order);
        PageInfo<Department> departMentVOPageInfo = new PageInfo<Department>(departments);
        return  departMentVOPageInfo;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/18
     * @Description 根据公司ID和父部门ID查询部门  （使用递归查询）
     * @Param [id, parentId] 公司ID
     * @Return java.util.List<com.bosssoft.boss_exam_db.entity.Department>
     * @Exception //无
     * @since JDK 1.8
     **/
    @Override
    public List<CascaderVO> queryByCompany(String id,String parentId) {
        Example example = new Example(Department.class);
        Example.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(id)){
            criteria .andEqualTo("companyId", id);
        }
        if(!StringUtils.isEmpty(parentId)){
            criteria .andEqualTo("parentId", parentId);
        }
        criteria.andEqualTo("deleted",false);
        List<Department> departmentList = this.departmentMapper.selectByExample(example);
        List<CascaderVO> cascaderVOS=null;
        if(departmentList.size()!=0&&departmentList!=null){
            cascaderVOS= departmentList.stream().map(department -> {
                List<CascaderVO> chidrenCascader = queryByCompany(id, department.getId());
                return new CascaderVO(department.getId(), department.getDepartmentName(), chidrenCascader);
            }).collect(Collectors.toList());
        }
        return cascaderVOS;
    }
}
